/**
 * 
 */
package neptune.web.client.view;

import neptune.web.client.controller.ExperimentNodeCommandHandler;
import neptune.web.client.model.SerializableExperiment;
import neptune.web.client.services.experiment.ExperimentNodesDataProvider;
import neptune.web.client.view.components.ExperimentAdministrationPanel;
import neptune.web.client.view.components.NodesManager;
import neptune.web.client.view.components.TopologyPanel;
import neptune.web.client.view.components.UserTable;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.SourcesTabEvents;
import com.google.gwt.user.client.ui.TabListener;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Roberto Bifulco [info@robertobifulco.it, robertobifulco.it]
 * 
 */
public class ExperimentView extends View {

	public static final String EXP_NAME = "expName";
	public static final String TOPOLOGY_FILE = "topologyFile";
	public static final String DEFAULT_TOKEN = "Experiment";
	private TabPanel mainPanel;

	private TopologyPanel topologyPanel;
	private NodesManager nodesManager;
	private String expName;
	private ExperimentAdministrationPanel expAdministrationPanel;

	public ExperimentView() {
		// Setting view params
		try {
			this.setName(DEFAULT_TOKEN);
		} catch (DuplicatedTokenException e) {
			ConsoleManager.addConsoleDebugMessage(e.getMessage(), true);
		}

		mainPanel = new TabPanel();
		mainPanel.add(buildExperimentDataPanel(), "Experiment");
		mainPanel.add(buildTopologyPanel(), "Topology");
		mainPanel.add(buildActiveNodesPanel(), "Active Nodes");

		mainPanel.selectTab(0);

		// Styles
		mainPanel.setStyleName("ExperimentView");
		mainPanel.getTabBar().setStyleName("ExperimentView-tab");
		mainPanel.getDeckPanel().setStyleName("ExperimentView-bottom");

		mainPanel.setSize("100%", "100%");

		// Tab listener
		mainPanel.addTabListener(new TabListener() {
			public void onTabSelected(SourcesTabEvents sender, int tabIndex) {
			}

			public boolean onBeforeTabSelected(SourcesTabEvents sender,
					int tabIndex) {
				// nodes manager selected
				if (tabIndex == 2) {
					nodesManager.refreshData();
				}
				return true;
			}
		});

		initWidget(mainPanel);
	}

	private Widget buildExperimentDataPanel() {
		expAdministrationPanel = new ExperimentAdministrationPanel();
		return expAdministrationPanel;
	}

	private Composite buildTopologyPanel() {
		topologyPanel = new TopologyPanel();
		return topologyPanel;
	}

	private Widget buildActiveNodesPanel() {
		VerticalPanel vPanel = new VerticalPanel();
		SimplePanel panel = new SimplePanel();
		nodesManager = new NodesManager(400);
		panel.setWidget(nodesManager);

		HTML help = new HTML("From this page you can manage "
				+ "experiment's active nodes. Active nodes are"
				+ " the experiment "
				+ "topology's nodes actually allocated and runnig.");
		help.setStylePrimaryName("ExperimentView-summaryHelp");

		vPanel.add(help);
		vPanel.add(panel);

		return vPanel;
	}

	public String getExperimentName() {
		return expName;
	}


	/**
	 * Carica l'esperimento nel topology panel
	 * @param exp
	 */
	public void setExperimentData(SerializableExperiment exp) 
	{
		expName = exp.getExperimentId();
		// Aggiornamento dei dati dell'administratio panel
		expAdministrationPanel.setData(exp);
		// Aggiornamento dei dati del topology panel
		topologyPanel.setExpName(exp.getExperimentId());
		topologyPanel.updateTopology(exp.getTopology());

		// Impostazione NodesManager
		nodesManager.reset();
		nodesManager.setDataProvider(new ExperimentNodesDataProvider(exp
				.getExperimentId()));
		nodesManager.addNodeCommandHandler(new ExperimentNodeCommandHandler(exp
				.getExperimentId()));
	}
	
	public void setXMLexperimentTopology(String xmlTopology)
	{
		this.topologyPanel.updateTopology(xmlTopology);
	}
	

	public void resetView() {
		expName = "";
		expAdministrationPanel.reset();
		topologyPanel.reset();
		nodesManager.reset();
		mainPanel.selectTab(0);
	}

	public void setExpetimentTabActive() {
		mainPanel.selectTab(0);
	}

	public void setExperimentState(String state) {
		expAdministrationPanel.setExperimentState(state);
	}

	public UserTable getUserTable() {
		return expAdministrationPanel.getUserTable();
	}
}